package com.hzxy.yiyid.web.module.service.impl;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzxy.yiyid.web.common.base.BaseResult;
import com.hzxy.yiyid.web.module.controller.vo.SolarTermsVo;
import com.hzxy.yiyid.web.module.repository.SolarTermsMapper;
import com.hzxy.yiyid.web.module.repository.entity.SolarTerms;
import com.hzxy.yiyid.web.module.service.SolarTermsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SolarTermsServiceImpl extends ServiceImpl<SolarTermsMapper, SolarTerms> implements SolarTermsService {
    private static final Logger logger = LoggerFactory.getLogger(SolarTermsServiceImpl.class);
    @Override
    public BaseResult<?> getSolarInfo() {
        //设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前日期
        Date current = new Date();
        String currentTime = sdf.format(current);

        //获取数据库日期列表
        List<Date> dbTime = list(null).stream()
                .map(solar -> solar.getSolarTime())
                .collect(Collectors.toList());

        //设置封装返回格式
        SolarTermsVo solarTermsVo = new SolarTermsVo();
        List<List<SolarTerms>> result = new ArrayList<>();
//        SolarTerms solarTerms = new SolarTerms();
        //当前日期和数据库所有日期比较差值
        try {
            for (Date date : dbTime) {
                //如果当前日期早于数据库日期
                if(current.compareTo(date)<0){
                    String dbCurrentTime = sdf.format(date);

                    //当前时间
                    Date start = sdf.parse(currentTime);
                    Long startT = start.getTime();
                    //数据库时间
                    Date db = sdf.parse(dbCurrentTime);
                    Long dbT = db.getTime();

                    //相减获取天数差数
                    Long num = (dbT-startT)/24/60/60/1000;
                    System.out.println("当前日期"+ currentTime +"和数据库时间" +dbCurrentTime + "相差" + num + "天");
                    logger.info("当前日期"+ currentTime +"和数据库时间" +dbCurrentTime + "相差" + num + "天");

                    //封装返回的条件
                    LambdaQueryWrapper<SolarTerms> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(SolarTerms::getSolarTime,date);
                    List<SolarTerms> list = list(queryWrapper);
                    list.get(0).setRemainingDays(num.intValue());
                    result.add(list);


                }
            }
        } catch (Exception e) {
            logger.error("时间格式异常！请重试");
            e.printStackTrace();
        }

        //获取节气日期截取
        solarTermsVo.setSolarTerms(result);
        //获取阴历
        DateTime dateTime = new DateTime(current);
        ChineseDate chineseDate = new ChineseDate(dateTime);
        System.out.println(chineseDate.toString());
        solarTermsVo.setLunaraClendar(chineseDate.toString());

        //获取星期
        solarTermsVo.setWeek(getWeek());

        //今日日期
        solarTermsVo.setNow(current);

        /**
         * 日
         */
        // 创建一个Calendar对象
        Calendar calendar = Calendar.getInstance();

        // 获取日期中的日
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        solarTermsVo.setDay(day);
        return BaseResult.success(solarTermsVo);
    }

    /**
     * 获取今天星期几
     * @return
     */
    public String getWeek(){
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        // 获取星期几
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        // 将数字转换为对应的星期几字符串
        String weekday = "";
        switch (dayOfWeek) {
            case Calendar.SUNDAY:
                weekday = "星期日";
                break;
            case Calendar.MONDAY:
                weekday = "星期一";
                break;
            case Calendar.TUESDAY:
                weekday = "星期二";
                break;
            case Calendar.WEDNESDAY:
                weekday = "星期三";
                break;
            case Calendar.THURSDAY:
                weekday = "星期四";
                break;
            case Calendar.FRIDAY:
                weekday = "星期五";
                break;
            case Calendar.SATURDAY:
                weekday = "星期六";
                break;
        }
        return weekday;
    }

}
